package com.nexus.all.controller;

import com.alibaba.excel.EasyExcel;
import com.nexus.all.dto.UserUpdateDTO;
import com.nexus.all.dto.excel.UserExcelData;
import com.nexus.all.listener.excel.UserCheckListener;
import com.nexus.all.listener.excel.UserImportListener;
import com.nexus.all.service.UserService;
import com.nexus.all.vo.UserVO;
import com.nexus.bean.annotation.PassLogin;
import com.nexus.bean.annotation.valid.AddGroup;
import com.nexus.bean.annotation.valid.OtherGroup;
import com.nexus.bean.annotation.valid.UpdateGroup;
import com.nexus.bean.model.UserInfoVO;
import com.nexus.bean.result.Result;
import com.nexus.bean.utils.BeanUtils;
import com.nexus.redis.utils.UserUtil;
import com.nexus.web.container.UserContainer;
import com.nexus.web.controller.CommonController;
import com.nexus.web.excel.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zxt
 */
@Slf4j
@RequestMapping("user")
@RestController
@AllArgsConstructor
@Api(tags = "用户管理接口")
public class UserController extends CommonController {

    private UserService userService;

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("get")
    public Result<UserInfoVO> get() {
        UserInfoVO user = UserUtil.getUser(null);
        UserVO userVO = userService.getById(user.getId());
        UserInfoVO userInfoVO = BeanUtils.beanCopy(userVO, UserInfoVO.class);
        UserContainer.putUserInfo(userInfoVO);
        return Result.newSuccess(userInfoVO);
    }

    @ApiOperation(value = "获取所有用户信息", notes = "获取所有用户信息")
    @PostMapping("getAllUser")
    @PassLogin
    public Result<List<UserVO>> getAllUser() {
        List<UserVO> userVOList = userService.getAllUser();
        return Result.newSuccess(userVOList);
    }


    @ApiOperation(value = "更新密码", notes = "更新密码")
    @PostMapping("updatePassword")
    public Result updatePassword(@RequestBody @Validated(value = AddGroup.class) UserUpdateDTO dto) {
        dto.setUserId(getUser().getId());
        userService.updatePassword(dto);
        return Result.newSuccess();
    }

    @ApiOperation(value = "更新头像", notes = "更新头像")
    @PostMapping("updateAvatar")
    public Result updateAvatar(@RequestBody @Validated(value = UpdateGroup.class) UserUpdateDTO dto) {
        dto.setUserId(getUser().getId());
        userService.updateAvatar(dto);
        return Result.newSuccess();
    }

    @ApiOperation(value = "更新昵称、邮箱", notes = "更新昵称、邮箱")
    @PostMapping("updateNameEmail")
    public Result updateNameEmail(@RequestBody @Validated(value = OtherGroup.class) UserUpdateDTO dto) {
        dto.setUserId(getUser().getId());
        userService.updateNameEmail(dto);
        return Result.newSuccess();
    }

    @ApiOperation(value = "下载模板", notes = "下载模板")
    @GetMapping("downloadTemplate")
    public void downloadTemplate() {
        ExcelUtil.simpleWrite("用户导入模板", "用户信息", UserExcelData.class, new ArrayList(), getResponse());
    }

    @ApiOperation(value = "校验excel数据", notes = "校验excel数据")
    @PostMapping("checkExcelData")
    public Result<List<String>> checkExcelData(@RequestParam("file") MultipartFile file) {
        try {
            UserCheckListener listener = new UserCheckListener(userService, getUser().getId());
            EasyExcel.read(file.getInputStream(), UserExcelData.class, listener).sheet().doRead();
            return Result.newSuccess(listener.getErrorList());
        } catch (Exception e) {
            log.info("异常信息", e);
            return Result.newError("校验失败！");
        }
    }

    @ApiOperation(value = "导入excel数据", notes = "导入excel数据")
    @PostMapping("importExcelData")
    public Result importExcelData(@RequestParam("file") MultipartFile file) throws IOException {
        UserImportListener listener = new UserImportListener(userService, getUser().getId());
        EasyExcel.read(file.getInputStream(), UserExcelData.class, listener).sheet().doRead();
        return Result.newSuccess();
    }


}